
define(["../../js/conf.js"], function(conf){
    var scope = this, tabWidget;
    var gInfo = window.gInfo;
    var cls = {
        hover: "vm-tr-hover"
    }
    var topState = scope.topState;
    var state = scope.state = {
    }
    scope.pointGroups = conf.pointGroups;
    scope.unmark = function(point){
        if(gInfo.store){
            var filePath = point.photo && point.photo.path;
            var id = point.id;
            if(filePath){
                var newPath = filePath.replace(/([^\/]+)(\.\w+)$/, '$1_'+(+new Date)+'$2');
                gInfo.store.cp(filePath, newPath);
                delete scope.allPhotos[filePath];
                
                window.vehicleData.fileMap[id] = conf.pointMap[id].photo = null;
                if(topState.currentDir == window.imageDir){ //重新扫描imageDir，同时更新filenav, main
                    $$.emit("vm.initdir", window.imageDir);
                }
            }
        }
    }
    
    function safeUrl(url){
        return url = url.replace(/%/g, '%25').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/ /g, '%20').replace(/\\/g, '/');
    }
    var isMac = (navigator.platform === "Mac68K") || (navigator.platform === "MacPPC") || (navigator.platform === "Macintosh") || (navigator.platform === "MacIntel");
    scope.groupChange = function(i){ //切换大类tab
        scope.currentGroup = scope.pointGroups[i];
        scope.checks = [];
        /*tabWidget.ready(function(){
            $$("td>.vm-point-thumb").each(function(_img){
                if(!_img.getAttribute("src")){
                    var src = _img.getAttribute("href");
                    if(!src){
                        return;
                    }
                    _img.setAttribute("src", src.replace(".jpg", "s.jpg"));
                }
            });
        });*/
    }

    var activeTr = null, isConflict = false; //拖放状态

    scope.routeInit = function(){
        if(topState.mode == 'audit' && window.imageDir && topState.currentDir != window.imageDir){
            topState.currentDir = window.imageDir;
            $$.emit("vm.initdir", window.imageDir);
        }
        topState.mode = 'marker';
        $$.rootWidget.refresh();
    }
    
    scope.init = function(widget){
        tabWidget = widget.children[1];
        $$.widget($$(".util-tree")).ready(function(){
            var arr = [
                $$(".photo-grid")[0]
            ];
            widget.$root.find("tbody").each(function(){
                arr.push(this);
            });
            window.dragula(arr, {
                copySortSource: false,
                revertOnSpill: true,
                accepts: function (el, target, source, sibling) {
                    if(sibling && sibling.tagName.toLowerCase() == 'tr' && sibling != activeTr){
                        isConflict = false;
                    }
                    return !activeTr || !isConflict;
                    return true; // elements can be dropped in any of the `containers` by default
                },
                invalid: function (el, handle) {
                    return false; // don't prevent any drags from initiating by default
                },
                moves: function (el, source, handle, sibling) {
                    return el.tagName.toLowerCase() == 'div'; // elements are always draggable by default
                }
            }).on('shadow', function(el, target, source, sibling){
                if(activeTr){
                    $$.classList.remove(activeTr, cls.hover);
                }
                if(sibling){
                    var pointId = sibling.getAttribute("data-point");
                    var point = conf.pointMap[pointId];
                    if(point){
                        isConflict = !!point.photo; // 已分配点位图
                        activeTr = sibling;
                        $$.classList.add(activeTr, cls.hover);
                    }
                }
            }).on('drop', function(el, target, source, sibling){
                if(activeTr){
                    // 移动生效
                    var pointId = activeTr.getAttribute("data-point");
                    var point = conf.pointMap[pointId];
                    var filePath = decodeURIComponent($$(el).attr("data-path").replace(/\\/g, '/'));
                    if(!window.imageDir){
                        alert("No imageDir");
                        return;
                    }
                    if(gInfo.store){
                        var newPath = window.imageDir + "/" + scope.currentGroup.label + "/" + point.id + ".jpg";
                        gInfo.store.cp(filePath, newPath);
                        scope.allPhotos[newPath] = $$.extend({}, scope.allPhotos[filePath], {
                            url: safeUrl(newPath),
                            path: newPath,
                            name: point.id + ".jpg",
                            basename: point.id + ".jpg"
                        });
                        delete scope.allPhotos[filePath];
                    }
                    $$.emit("point.mark", point.id, newPath); //设置vehicleData.fileMap
                    point.photo = window.vehicleData.fileMap[point.id];
                    scope.$refresh();
                }
            }).on('dragend', function(){
                if(activeTr){
                    $$.classList.remove(activeTr, cls.hover);
                    activeTr = null;
                    isConflict = false;
                }
            });
        });
    };
});
